/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rafkind.crawl.data.team;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;

/**
 *
 * @author drafkind
 */
public class TextMenu<T> {
  private List<T> data;
  private String prompt;
  private boolean allowCancel;

  public TextMenu(String prompt, List<T> data,  boolean allowCancel) {
    this.data = data;
    this.prompt = prompt;
    this.allowCancel = allowCancel;
  }
  
  public T get() {
    while (true) {
      System.out.println(prompt);
      
      if (allowCancel) {
	System.out.println("(0) - Cancel");
      }
      int index = 1;
      for (T t : data) {
	System.out.println("(" + index + ") - " + t);
	index++;
      }
      
      System.out.print("Your Choice? ");
      
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      try {
	index = -1;
	String input = in.readLine();
      
	index = Integer.parseInt(input);
      } catch (Exception ex) {	
      }
      
      if (index == 0 && allowCancel) {
	return null;
      } else {
	if (index >= 1 && index <= data.size()) {
	  return data.get(index-1);
	}
      }
    }
  }
}
